//
//  BBHomeMenuView.m
//  BlockBrowser
//
//  Created by Mac on 2018/3/28.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBHomeMenuView.h"
@interface BBHomeMenuView()

@end
@implementation BBHomeMenuView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configSubView];
    }
    return self;
}
- (void)setDataSouce:(NSArray<BBHomeMenuModel *> *)dataSouce{
    _dataSouce = dataSouce;
    [self configDataSouceView];
}
#pragma mark --subView
- (void)configSubView{
    self.backgroundColor = [UIColor whiteColor];
    if ([BBThemeColorManger manger].type == BBThemeColorStyleBlacklook) {
        self.backgroundColor = [BBThemeColorManger manger].themeColor;
    }
    NSArray * array = @[
  @{@"title":@"新浪",@"imageName":@"新浪"},
  @{@"title":@"百度",@"imageName":@"baidu"},
  @{@"title":@"微博",@"imageName":@"weibofei"},
  @{@"title":@"二手车",@"imageName":@"瓜子"},
  @{@"title":@"同城",@"imageName":@"58"},
  @{@"title":@"淘宝",@"imageName":@"tao"},
  @{@"title":@"携程",@"imageName":@"xiecheng"},
  @{@"title":@"苏宁",@"imageName":@"苏宁"},
  @{@"title":@"优酷",@"imageName":@"优酷"},
  @{@"title":@"积分",@"imageName":@"积分"}];
    __weak typeof(self) weakSelf =  self;
    CGFloat with = UIScreenWidth / 5;
    for (NSInteger i = 0; i < array.count; i ++) {
        NSDictionary * dic = array[i];
        NSString * imageName = dic[@"imageName"];
        NSString * title = dic[@"title"];
        imageName = @"";
        title = @"";
        STMenuControl * control = [[STMenuControl alloc] initWithFrame:CGRectMake(0, 20, with, 80) imageName:imageName title:title titleColor:SecendTextColor];
        [self addSubview:control];
        control.left = (i % 5 ) *with;
        control.top = 20 + (i / 5 ) *80;
        self.height = control.bottom  +20;
        [control setOnSelctedControl:^(STMenuControl *control) {
            if (weakSelf.onSlectedMenuHandle) {
                weakSelf.onSlectedMenuHandle(title);
            }
        }];
    }
   
}
- (void)configDataSouceView{
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
    CGFloat with = UIScreenWidth / 5;
    __weak typeof(self) weakSelf =  self;
    for (NSInteger i = 0; i < self.dataSouce.count; i ++) {
        BBHomeMenuModel * model = self.dataSouce[i];
        NSString * imageName = model.icon;
        NSString * title = model.name;
        STMenuControl * control = [[STMenuControl alloc] initWithFrame:CGRectMake(0, 20, with, 80) imageName:imageName title:title titleColor:SecendTextColor];
        [control.imageButton sd_setImageWithURL:[NSURL URLWithString:imageName] forState:UIControlStateNormal];
        [control setOnSelctedControl:^(STMenuControl *control) {
            if (weakSelf.onSlectedMenuHandle) {
                weakSelf.onSlectedMenuHandle(title);
            }
        }];
        [self addSubview:control];
        control.left = (i % 5 ) *with;
        control.top = 20 + (i / 5 ) *80;
        self.height = control.bottom  +20;
    }
}
#pragma mark --Action Method

@end
